package cn.est.util;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpClientUtil {
    /**
     * 发送get请求返回字符串的结果
     * @param getUrl
     * @return
     * @throws IOException
     */
    public static String sendGet(String getUrl) throws IOException {
        // 一个 httpClient 就是一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 一个get请求的对象
        HttpGet httpGet = new HttpGet(getUrl);
        CloseableHttpResponse response = httpClient.execute(httpGet);
        try {
            System.out.println(response.getStatusLine());
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity);
        }finally {
            response.close();
        }
    }
}
